package com.bdqn.controller;

import com.bdqn.pojo.User;
import com.bdqn.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Date;
import java.util.List;

/**
 * @version 2024-09-13
 * @Author:T369曹任柱
 */

@Controller
public class ExampleController {
    @Resource
    private UserService userService;

    @GetMapping(value = "/hello/{id}")
    public String getUser(@PathVariable("id")Long usrId, Model model){
        User user=userService.getUser(usrId);
        System.out.println(user);
        model.addAttribute("user",user);
        return "demo/hello";
    }


    @GetMapping(value = "/string")
    public String getString(Model model, HttpServletRequest request){
        model.addAttribute("userName","ktjiaoyu");
        request.setAttribute("test","request");
        request.getSession().setAttribute("test","session");
        model.addAttribute("data",new Date());
        return "demo/string";
    }

    @GetMapping(value = "/inline")
    public String inline(Model model){
        model.addAttribute("userName","ktjiaoyu");
        return "demo/inline";
    }

    @GetMapping(value = "/if")
    public String ifunless(Model model){
        model.addAttribute("flag","yes");
        return "demo/if";
    }
    @GetMapping(value = "/list")
    public String list(Model model){
        List<User>list=userService.findAllUsers();
        model.addAttribute("users",list);
        return "demo/list";
    }


}

